/**
 * 
 */
package smallChanges.statements;

import net.sourceforge.pmd.ast.ASTBlock;
import net.sourceforge.pmd.ast.ASTDoStatement;
import net.sourceforge.pmd.ast.ASTExpression;
import net.sourceforge.pmd.ast.ASTIfStatement;
import net.sourceforge.pmd.ast.ASTStatement;
import net.sourceforge.pmd.ast.ASTSynchronizedStatement;
import net.sourceforge.pmd.ast.ASTWhileStatement;
import net.sourceforge.pmd.ast.JavaParserVisitorAdapter;
import smallChanges.Tokenization.SCToken;
import smallChanges.Tokenization.TokenSet;

/**
 * @author jspacco
 *
 */
public class ConditionalStatementVisitor extends JavaParserVisitorAdapter
{
	private TokenSet tokenSet;
	
	public ConditionalStatementVisitor(TokenSet tokenSet) {
		this.tokenSet=tokenSet;
	}

	/* (non-Javadoc)
	 * @see net.sourceforge.pmd.ast.JavaParserVisitorAdapter#visit(net.sourceforge.pmd.ast.ASTExpression, java.lang.Object)
	 */
	@Override
	public Object visit(ASTExpression node, Object data)
	{
		if (data instanceof ConditionalStatementHeader) {
			ConditionalStatementHeader header=(ConditionalStatementHeader)data;
            MultiTokenCodeRange2D range=new MultiTokenCodeRange2D(tokenSet);
			range.setCoordinatesFromNode(node);
			header.setCondition(range);
			// grab the token immediately following the condition into the token stream
			// this should be the right paren, for example, given:
			// if (x > y) {
			//   blah;
			// }
			//
			// we are processing the x>y part, but we need to include the right paren as
			// part of the if-else header
			SCToken rightParen=tokenSet.getFollowingTokens(node).getFirst();
			header.setEndToken(rightParen);
			return header;
		}
		return super.visit(node, data);
	}

	/* (non-Javadoc)
	 * @see net.sourceforge.pmd.ast.JavaParserVisitorAdapter#visit(net.sourceforge.pmd.ast.ASTForStatement, java.lang.Object)
	 */
	@Override
	public Object visit(ASTWhileStatement node, Object data)
	{
		if (data instanceof ConditionalStatementHeader) {
			ConditionalStatementHeader header=(ConditionalStatementHeader)data;
			header.setBeginCoordsFromNode(node);
			return super.visit(node, header);
		}
		return super.visit(node, data);
	}
	
	@Override
    public Object visit(ASTDoStatement node, Object data)
    {
        if (data instanceof ConditionalStatementHeader) {
            ConditionalStatementHeader header=(ConditionalStatementHeader)data;
            header.setBeginCoordsFromNode(node);
            return super.visit(node, header);
            // return header;
        }
        return super.visit(node, data);
    }
	
	/* (non-Javadoc)
	 * @see net.sourceforge.pmd.ast.JavaParserVisitorAdapter#visit(net.sourceforge.pmd.ast.ASTIfStatement, java.lang.Object)
	 */
	@Override
	public Object visit(ASTIfStatement node, Object data)
	{
		if (data instanceof ConditionalStatementHeader) {
			ConditionalStatementHeader header=(ConditionalStatementHeader)data;
			header.setBeginCoordsFromNode(node);
			return super.visit(node, header);
		}
		return super.visit(node, data);
	}
	
	@Override
    public Object visit(ASTSynchronizedStatement node, Object data)
    {
	    if (data instanceof ConditionalStatementHeader) {
	        ConditionalStatementHeader header=(ConditionalStatementHeader)data;
            header.setBeginCoordsFromNode(node);
            node.childrenAccept(this, header);
            return header;
	    }
        return super.visit(node, data);
    }

    /* (non-Javadoc)
	 * @see net.sourceforge.pmd.ast.JavaParserVisitorAdapter#visit(net.sourceforge.pmd.ast.ASTStatement, java.lang.Object)
	 */
	@Override
	public Object visit(ASTStatement node, Object data)
	{
		if (data instanceof ConditionalStatementHeader) {
			ConditionalStatementHeader stmt=(ConditionalStatementHeader)data;
			return data;
		}
		return super.visit(node, data);
	}

    @Override
    public Object visit(ASTBlock node, Object data)
    {
        if (data instanceof ConditionalStatementHeader) {
            return data;
        }
        return super.visit(node, data);
    }
	
	
}
